#include "pilereels.h"

void pileReels::addX(const QString c){
    if(c == ","){
        if(dot == false){
            dot = true; // voire peut etre bloquer le bouton virgule
            decimal = 1;
            pA->setText(getQStringX());
        }
    }
    else if(c == "-"){
        negatif = !negatif;
         pA->setText(getQStringX());
    }
    else{
        if (dot == false && (x*10+c.toInt()) < MAX){
            x = x*10+c.toInt();
            precision ++;
        }
        else if(dot == true && decimal > MAX_DECIMAL){
            decimal *= 0.1;
            precision ++;
            x = x + c.toInt()*decimal;
        }
        pA->setText(getQStringX());
    }
}

void pileReels::iniX(){
    MAX_DECIMAL = 0.0000000000000001;
    dot = false;
    negatif = false;
    decimal = 0,1;
    precision = 0;
    x = 0;
    updateAfficheur();
}

double pileReels::getX()const{
    if (negatif) return -x;
    else return x;
}

QString pileReels::getQStringX()const{
    return QString().setNum(getX(),'g',precision);
}

void pileReels::back(){
    if (dot == false){
        x = floor(x / 10);
        precision --;
    }
    pA->setText(getQStringX());
}

void pileReels::updateAfficheur()const{
    // met a jour l'afficheur pour la pile
    QString h = "";
    QStack<double>::const_iterator i;
    for (i = begin(); i != end(); ++i)
        h += QString().setNum(*i,'g',len(*i)) + "\n";
    aPile->setText(h);
}

void pileReels::sauvegarder()const{
    QSettings settings("mesOptions.ini", QSettings::IniFormat);
    int taille = this->size();
    settings.beginGroup("PileReels");
    settings.setValue("taille", taille);
    settings.beginWriteArray("Valeur");
    const double *data = this->data();
    for(int i=0; i!=taille; i++){
       settings.setArrayIndex(i);
       settings.setValue("Reel", data[i]);
   }

    settings.endArray();
    settings.endGroup();
}

QString toQString(const double x){
    return QString().setNum(x,'g',len(x));
}
